Page({
  data: {
    order: {
      _id: '',
      serviceName: '',
      serviceTime: '',
      address: '',
      contactName: '',
      contactPhone: '',
      specialRequest: '',
      servicePrice: 0,
      otherFee: 0,
      totalPrice: 0,
      paymentMethod: '',
      paymentTime: '',
      status: '',
      createTime: '',
      updateTime: '',
      hasReviewed: false
    }
  },

  onLoad(options) {
    // 获取订单ID
    const orderId = options.id
    
    // 加载订单详情数据
    this.loadOrderDetail(orderId)
  },

  // 加载订单详情
  loadOrderDetail(orderId) {
    // 模拟数据，实际项目中应该从云数据库获取
    const mockData = {
      _id: orderId,
      serviceName: '专业护理服务',
      serviceTime: '2025-09-23 09:00',
      address: '北京市朝阳区某某小区1号楼101',
      contactName: '张先生',
      contactPhone: '13800138000',
      specialRequest: '老人有轻度糖尿病，需要注意饮食',
      servicePrice: 150,
      otherFee: 0,
      totalPrice: 150,
      paymentMethod: '微信支付',
      paymentTime: '2025-09-22 14:30:45',
      status: 'paid',
      createTime: '2025-09-22 14:25:30',
      updateTime: '2025-09-22 14:30:45',
      hasReviewed: false
    }
    
    this.setData({
      order: mockData
    })
    
    // 实际项目中的云数据库查询示例
    /*
    const db = wx.cloud.database()
    db.collection('orders')
      .doc(orderId)
      .get()
      .then(res => {
        this.setData({
          order: res.data
        })
      })
      .catch(err => {
        console.error('获取订单详情失败', err)
        wx.showToast({
          title: '加载失败，请重试',
          icon: 'none'
        })
      })
    */
  },

  // 获取状态文本
  getStatusText(status) {
    const statusMap = {
      'pending_payment': '待付款',
      'paid': '已支付',
      'in_progress': '服务中',
      'completed': '已完成',
      'cancelled': '已取消'
    }
    return statusMap[status] || status
  },

  // 获取状态颜色
  getStatusColor(status) {
    const colorMap = {
      'pending_payment': '#FF9D4C',
      'paid': '#4080FF',
      'in_progress': '#4080FF',
      'completed': '#07C160',
      'cancelled': '#888888'
    }
    return colorMap[status] || '#333333'
  },

  // 获取当前步骤
  getCurrentStep(status) {
    const stepMap = {
      'pending_payment': 0,
      'paid': 1,
      'in_progress': 2,
      'completed': 3,
      'cancelled': -1
    }
    return stepMap[status] || 0
  },

  // 支付订单
  onPayOrder() {
    wx.navigateTo({
      url: `/pages/order/payment?id=${this.data.order._id}`
    })
  },

  // 取消订单
  onCancelOrder() {
    wx.showModal({
      title: '取消订单',
      content: '确定要取消该订单吗？',
      confirmText: '确定取消',
      confirmColor: '#FF0000',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '处理中...',
          })
          
          // 模拟取消订单，实际项目中应该调用云函数
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '订单已取消',
              icon: 'success'
            })
            
            // 更新订单状态
            this.setData({
              'order.status': 'cancelled',
              'order.updateTime': new Date().toISOString()
            })
          }, 1000)
          
          // 实际项目中的云函数调用示例
          /*
          wx.cloud.callFunction({
            name: 'cancelOrder',
            data: { orderId: this.data.order._id },
            success: () => {
              wx.hideLoading()
              wx.showToast({
                title: '订单已取消',
                icon: 'success'
              })
              this.loadOrderDetail(this.data.order._id)
            },
            fail: (err) => {
              wx.hideLoading()
              console.error('取消订单失败', err)
              wx.showToast({
                title: '取消失败，请重试',
                icon: 'none'
              })
            }
          })
          */
        }
      }
    })
  },

  // 评价订单
  onReviewOrder() {
    wx.navigateTo({
      url: `/pages/review/create?orderId=${this.data.order._id}`
    })
  },

  // 分享功能
  onShareAppMessage() {
    return {
      title: `我的养老服务订单 - ${this.data.order.serviceName}`,
      path: `/pages/order/detail?id=${this.data.order._id}`
    }
  }
})